<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="菜单名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryKeyword">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="menuList" highlight-current-row v-loading="listLoading" @selection-change="selectChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="40">
      </el-table-column>
      <el-table-column prop="name" label="菜单">
      </el-table-column>
      <el-table-column prop="url" label="路径">
      </el-table-column>
      <el-table-column prop="icon" label="图标">
      </el-table-column>
      <el-table-column prop="parent.name" label="上级菜单">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
      <el-pagination style="float: right;margin-top: 10px;" @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 50]"
                     :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!-- 对话框 添加/修改 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" @close="dialogClose">
      <el-form :model="form" :rules="addFormRules" label-width="80px">
        <el-input v-model="form.id" autocomplete="off" type="hidden"></el-input>
        <el-form-item label="菜单" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="访问路径">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="form.icon" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="上级菜单">
          <!-- <el-input v-model="form.managerId" autocomplete="off" style="width: 70%;"></el-input> -->
          <el-select v-model="form.parent" placeholder="请选择" clearable value-key="id">
            <el-option :label="menu.name" :value="menu" v-for="menu in menus"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSure">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      menuList: {
      },
      currentPage: 1,
      pageSize: 10,
      // 总记录数
      total: 0,
      listLoading: false,
      sels: [],//列表选中列
      dialogFormVisible: false,
      form: {},
      addFormRules: {
        name: [
          { required: true, message: '请输入菜单', trigger: 'blur' }
        ]
      },
      menus: [],
      dialogTitle:''
    }
  },
  created() {
    this.loadDataPage();
    this.getDepts();
  },
  methods: {
    loadDataPage() {
      let pageQuery = {
        keyword: this.filters.keyword,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }
      this.$http.post("/menu", pageQuery).then(res => {
        this.menuList = res.data.data.pageList
        this.total = res.data.data.count
      })
    },

    //改变每页总条数触发事件
    handleSizeChange(val) {
      this.pageSize = val
      this.loadDataPage();
    },
    //改变当前页触发事件
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadDataPage();
    },
    queryKeyword() {
      this.currentPage = 1;
      this.pageSize = 10;
      this.loadDataPage();
    },
    //打开对话框
    handleAdd() {
      this.dialogFormVisible = true
      this.dialogTitle = "添加";
    },
    //取消对话框
    handleClose() {
      this.dialogFormVisible = false;
      this.form = {};
    },
    //确定 发送添加/修改请求
    handleSure() {
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.$http.put('/menu', this.form).then((res) => {
          if (res.data.success) {
            this.$message({
              message: res.data.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
          this.dialogFormVisible = false;
          this.loadDataPage();
          this.form = {};
        });
      });
    },
    //编辑
    handleEdit(index, row) {
      this.dialogTitle = "修改"
      this.dialogFormVisible = true;
      // 复制数据，避免修改数据后点击取消仍会改变当前行数据的情况
      let copy = Object.assign({}, row)
      this.form = copy;//数据回显
    },
    // X
    dialogClose() {
      this.form = {}
    },
    //删除
    handleDel(row) {
      this.$confirm('确认删除吗？', '提示', {}).then(() => {
        this.$http.delete('/menu/' + row.id).then((res) => {
          if (res.data.success) {
            this.$message({
              message: res.data.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
          this.loadAfterDeleteLastData();
          this.loadDataPage();//刷新
        });
      });
    },
    //批量选择
    selectChange(val) {
      let delArray = [];
      for (let i = 0; i < val.length; i++) {
        delArray[i] = val[i].id
      }
      this.sels = delArray;
    },
    //批量删除
    batchRemove() {
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.patch('/menu',this.sels).then((res) => {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.loadAfterDeleteLastData();
          this.loadDataPage();
        });
      })
    },
    //获取员工列表，用于多选
    getDepts() {
      this.$http.get('/menu').then(res => {
        this.menus = res.data.data
      })
    },
    // 删除分页的最后一条数据后，跳到上一分页
    loadAfterDeleteLastData(){
      let deleteAfterPage = Math.ceil((this.total - 1) / this.pageSize)
      let deleteAfterCurrentPage = this.currentPage > deleteAfterPage ? deleteAfterPage : this.currentPage
      this.currentPage = deleteAfterCurrentPage < 1 ? 1 : deleteAfterCurrentPage
    }

  }
}

</script>

<style scoped></style>